package com.ruoyi.apm.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.apm.dao.DockDataMapper;
import com.ruoyi.apm.http.client.HttpClientHandler;
import com.ruoyi.apm.http.client.HttpSequenceHandle;
import com.ruoyi.apm.model.DockDataConfig;
import com.ruoyi.apm.model.HttpRequest;
import com.ruoyi.apm.model.HttpRequestLog;
import com.ruoyi.apm.model.HttpSequence;
import com.ruoyi.apm.service.DockDataService;
import com.ruoyi.common.annotation.ADDDataSource;
import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: youguoen
 * Date: 2023/12/28
 */
@Service
public class DockDataServiceImpl implements DockDataService {



    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW,rollbackFor = Exception.class)
//    @DataSource(DataSourceType.SLAVE)
    @ADDDataSource
    public void insertDataForMysql(HttpSequence httpSequence, HttpSequenceHandle httpSequenceHandle, String dataSource) {
        excuteInsert(httpSequence,httpSequenceHandle);
    }

    @Override
//    @DataSource(DataSourceType.CK)
    @ADDDataSource
    public void insertDataForCk(HttpSequence httpSequence, HttpSequenceHandle httpSequenceHandle, String dataSource) {
        excuteInsert(httpSequence,httpSequenceHandle);
    }

    public void excuteInsert(HttpSequence httpSequence, HttpSequenceHandle httpSequenceHandle){
        List<HttpRequest> requests = httpSequence.getHttpRequest();
        DockDataConfig dockDataConfig = httpSequence.getDockDataConfig();
        List<Map<String,Object>> list = new ArrayList<>();
        int i = 0;
        boolean cleanData = dockDataConfig.getIsClean()==1?true:false;
        boolean apiData = true;
        for(HttpRequest request : requests){
            i++;
            if(i==requests.size()){//获取数据api
                if(dockDataConfig.getIsWhile()==1){
                    String field = dockDataConfig.getWhileRule();
                    for(int m=0;m<Integer.MAX_VALUE;m++){
                        if(m==0){
                            if(!httpSequenceHandle.excuteOne(list,request,dockDataConfig,apiData,cleanData)){//已经是最大页码
                                break;
                            }
                            cleanData = false;
                        }else{
                            if(!httpSequenceHandle.excuteOne(list,request,dockDataConfig,apiData,cleanData)){//已经是最大页码
                                break;
                            }
                        }
                        request = httpSequenceHandle.getWhileRequest(request,field);
                    }
                }else{
                    httpSequenceHandle.excuteOne(list,request,dockDataConfig,apiData,cleanData);
                }
            }else{
                if(requests.size()==1){
                    httpSequenceHandle.excuteOne(list,request,dockDataConfig,apiData,cleanData);
                }else{
                    httpSequenceHandle.excuteOne(list,request,dockDataConfig,!apiData,cleanData);
                }

            }
        }
        if(list.size()>0){
            Map<String,Object> fieldMap = (Map<String,Object>) JSONObject.parse(dockDataConfig.getMappingRelation());
            httpSequenceHandle.insertData(dockDataConfig.getTableName(),fieldMap,list);
        }
    }
}
